package com.zzh.fullscreen;

import mymusic.MyMusic;
import test.Draw;
import test.MainRun;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Player extends Flyer implements KeyListener, MouseMotionListener {
    /**
     * 5,6,7
     */
    private int fireType = 5;
    private int life = 3;
    private int score = 0;
    private List<Bullet> bullets = null;
    private long keydelay = 0;
    private int firedelay = 200;
    static MyMusic music1;
    protected static BufferedImage image;

    static {
        music1 = new MyMusic(Player.class.getClassLoader()
                .getResource("music/arrow_shot.wav").getPath(), 0);
        try {
            image = ImageIO.read(Player.class.getClassLoader()
                    .getResourceAsStream("img/my_plane.png"));
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    public Player(int x, int y) {
        super(x, y, 0, 0);
        speed = 6;
        bullets = Collections.synchronizedList(new LinkedList<Bullet>());
        width = image.getWidth() * 2 / 3;
        height = image.getHeight() * 2 / 3;
        cx = x;
        cy = y;
        cwidth = width;
        cheight = height;
    }

    public List<Bullet> getBulletList() {
        return bullets;
    }

    public void setScoer(int sc) {
        score += sc;
    }

    public void upFireType() {
        if (fireType != 7) {
            fireType++;
        }
    }

    public void dowFireType() {
        if (fireType != 5) {
            fireType--;
        } else if (fireType == 5) {
            setLife();
        }
    }

    public void setLife() {
        life -= 1;
    }

    public int getLife() {
        return life;
    }

    public void addLife() {
        life += 1;
    }

    public void resetPosition() {
        x = 230;
        y = 756 - 64;
    }

    public void rest() {
        life = 3;
        score = 0;
        this.bullets.clear();
        resetPosition();
    }

    public int getScore() {
        return score;
    }

    public void paint(Graphics g) {
        g.drawImage(image, x, y, width, height, null);
        // g.drawRect(cx,cy,cwidth,cheight);
        // g.drawLine(this.x+width/2, 0,this.x+width/2, 700);
    }

    public void step() {
        if ((x + velx >= 0 && x + velx <= Draw.screenWidth - width)
                && (y + vely >= 0 && y + vely <= Draw.screenHeight - height))
            this.updataXY();
    }

    @Override
    public void keyPressed(KeyEvent ke) {
        //空格键暂停和继续游戏
        if (ke.getKeyCode() == KeyEvent.VK_SPACE) {
            if (MainRun.md.state == Draw.RUNNING) {
                MainRun.md.state = Draw.PAUSE;
                System.out.println("Game PAUSE");
                return;
            } else if (MainRun.md.state == Draw.PAUSE) {
                MainRun.md.state = Draw.RUNNING;
                System.out.println("Game RUNNING");
                return;
            }
        }
        // TODO Auto-generated method stub
        if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
            velx = -speed;
        } else if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
            velx = speed;
        }
        if (ke.getKeyCode() == KeyEvent.VK_UP) {
            vely = -speed;
        } else if (ke.getKeyCode() == KeyEvent.VK_DOWN) {
            vely = speed;
        }
        if (ke.getKeyCode() == KeyEvent.VK_J) {
            //shootting(ke);
        }
        // if(ke.getKeyCode()==KeyEvent.VK_K){
        // if(life>=0&&ke.getWhen()-keydelay>=firedelay)
        // {
        // new Thread(music1).start();
        //
        // bullets.add(new Bullet(3,this.x+width/2 ,this.y,Flyer.UP));
        // keydelay=ke.getWhen();
        // }
        // }
        // if(ke.getKeyCode()==KeyEvent.VK_L){
        // if(life>=0&&ke.getWhen()-keydelay>=firedelay)
        // {
        // new Thread(music1).start();
        //
        // bullets.add(new Bullet(7,this.x+width/2 ,this.y,Flyer.UP));
        // keydelay=ke.getWhen();
        // }
        // }
        // if(ke.getKeyCode()==KeyEvent.VK_SPACE)
        // {
        //
        // }
    }
    public void autoShoot(){
        if(System.currentTimeMillis() - keydelay >= firedelay){
            bullets.add(new Bullet(fireType, this.x + width / 2, this.y - height,
                    Flyer.UP));
            keydelay = System.currentTimeMillis();
        }
    }
    private void shootting(KeyEvent ke) {
        if (this.fireType == 5)
            firedelay = 300;
        else
            firedelay = 450;

        if (life >= 0 && ke.getWhen() - keydelay >= firedelay) {
            new Thread(music1).start();

            bullets.add(new Bullet(fireType, this.x + width / 2, this.y,
                    Flyer.UP));
            keydelay = ke.getWhen();
        }
    }

    @Override
    public void keyReleased(KeyEvent ke) {

        if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
            velx = 0;
        } else if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
            velx = 0;
        }
        if (ke.getKeyCode() == KeyEvent.VK_UP) {
            vely = 0;
        } else if (ke.getKeyCode() == KeyEvent.VK_DOWN) {
            vely = 0;
        }

    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseDragged(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        // int posx=e.getX();
        // int posy=e.getY();
        // if(this.x>posx){
        // this.velx=-this.speed;
        // }else if(this.x<posx){
        // this.velx=this.speed;
        // }else{
        // velx=0;
        // }
        //
        // if(this.y>posy){
        // this.vely=-this.speed;
        // }else if(this.y<posy){
        // this.vely=this.speed;
        // }else{
        // vely=0;
        // }

    }

}
